package com.cauli.manage.config.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cauli.manage.config.model.vo.ConfigVO;
import com.cauli.manage.config.model.vo.ConfigDetailVO;
import com.cauli.manage.config.model.dto.ConfigQueryDTO;
import com.cauli.manage.config.model.dto.ConfigUpdateDTO;
import com.cauli.manage.config.model.dto.UpdateSecuritySettingRequestDTO;
import com.cauli.manage.config.model.dto.UpdateSiteSettingRequestDTO;
import com.cauli.manage.config.model.entity.Config;
import com.cauli.manage.config.service.ConfigService;
import com.cauli.utils.util.ResultResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.qiwenshare.common.anno.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Cauli
 * @date 2022/12/15 15:08
 * @description 系统设置 前端控制器
 */
@Api(tags = "系统设置")
@ApiSort(1)
@RestController
@RequestMapping("/config")
public class ConfigController {
    @Autowired
    private ConfigService configService;

    public static final String CURRENT_MODULE = "系统设置";

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取系统设置息列表")
    @GetMapping("/list")
    public ResultResponse<Config> listConfig(ConfigQueryDTO configQueryDTO) {
        Page<Config> configPage = configService.getConfigPage(configQueryDTO);
        return ResultResponse.success().dataList(configPage.getRecords(), configPage.getTotal());
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据系统设置ID获取详细信息")
    @GetMapping("/{configId}")
    public ResultResponse<ConfigVO> getConfig(@PathVariable("configId") Long configId) {
        ConfigVO configVO = configService.getConfigById(configId);
        return ResultResponse.success().data(configVO);
    }

    @MyLog(operation = "修改系统设置", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 3)
    @ApiOperation("修改系统设置")
    @PutMapping
    public ResultResponse<Void> updateConfig(@Valid @RequestBody ConfigUpdateDTO configUpdateDTO) {
        configService.updateConfig(configUpdateDTO);
        return ResultResponse.success().message("修改成功");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("获取系统设置信息")
    @GetMapping("/get")
    public ResultResponse<ConfigDetailVO> getConfig(ConfigQueryDTO configQueryDTO) {
        ConfigDetailVO config = configService.getConfig(configQueryDTO);
        return ResultResponse.success().data(config);
    }

    @MyLog(operation = "修改登录设置", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 4)
    @ApiOperation("修改登录设置")
    @PutMapping("/security")
    public ResultResponse<Void> updateSecuritySetting(@Valid @RequestBody UpdateSecuritySettingRequestDTO settingRequestDTO) {
        ConfigDetailVO configDetailVO = new ConfigDetailVO();
        BeanUtils.copyProperties(settingRequestDTO, configDetailVO);
        configService.updateConfig(configDetailVO);
        return ResultResponse.success().message("修改成功");
    }

    @MyLog(operation = "修改站点设置", module = CURRENT_MODULE)
    @ApiOperationSupport(order = 5)
    @ApiOperation("修改站点设置")
    @PutMapping("/site")
    public ResultResponse<Void> updateSiteSetting(@Valid @RequestBody UpdateSiteSettingRequestDTO settingRequestDTO) {
        ConfigDetailVO configDetailVO = new ConfigDetailVO();
        BeanUtils.copyProperties(settingRequestDTO, configDetailVO);
        configService.updateConfig(configDetailVO);
        return ResultResponse.success().message("修改成功");
    }
}
